package com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.exam.adapter.SubjectImgAdapter;
import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.utils.AnswerCallback;
import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.utils.ScoreUtil;
import com.dayaokeji.rhythmschoolstudent.utils.ObjectUtils;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.server_api.domain.ExamQuestion;
import com.dayaokeji.server_api.domain.SubjectAnswer;

import butterknife.BindView;

/**
 * 判断
 */
public class JudgmentProblemFragment extends BaseSubjectFragment {

    @BindView(R.id.tv_subject_type)
    TextView tvSubjectType;
    @BindView(R.id.tv_subject_score)
    TextView tvSubjectScore;
    @BindView(R.id.tv_subject_content)
    TextView tvSubjectContent;
    @BindView(R.id.rv_subject_imgs)
    RecyclerView rvSubjectImages;
    @BindView(R.id.rg_answer_group)
    RadioGroup rgAnswerGroup;
    @BindView(R.id.rb_answer_success)
    RadioButton rbAnswerSuccess;
    @BindView(R.id.rb_answer_error)
    RadioButton rbAnswerError;
    @BindView(R.id.iv_subject_state)
    ImageView ivSubjectState;

    private ExamQuestion examQuestion;
    private final SubjectImgAdapter subjectImgAdapter = new SubjectImgAdapter();
    private SubjectAnswer subjectAnswer = new SubjectAnswer();
    private String answer;
    private String q1;
    private String q2;

    public static JudgmentProblemFragment newInstance(ExamQuestion examQuestion) {
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_EXAM_ENTITY,examQuestion);
        JudgmentProblemFragment fragment = new JudgmentProblemFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Nullable
    @Override
    protected View onCreate(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_judgment_problem, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    private void init() {
        examQuestion = (ExamQuestion) getArguments().getSerializable(EXTRA_EXAM_ENTITY);
        if (examQuestion == null) {
            ToastUtils.warning(R.string.data_incorrect);
            return;
        }
        subjectAnswer.setExamQuestionId(examQuestion.getId());
        setupView();
        setupViewData();
        setOnListnener();
    }

    private void setupView() {
        rvSubjectImages.setLayoutManager(new GridLayoutManager(getActivity(),4));
        rvSubjectImages.setAdapter(subjectImgAdapter);

        String answerOptions = examQuestion.getAnswerOptions();
        if (answerOptions != null) {
            String[] split = answerOptions.split(",");
            if (split.length >= 2) {
                q1 = split[0];
                q2 = split[1];

                rbAnswerSuccess.setText(q1);
                rbAnswerError.setText(q2);
            }
        }
    }

    private void setupViewData() {
        tvSubjectType.setText(getSubjectTypeStr(examQuestion.getType()));
        tvSubjectScore.setText(String.format("分值：%s 分", ScoreUtil.doubleTrans(examQuestion.getScore())));
        tvSubjectContent.setText(examQuestion.getContent());
        subjectImgAdapter.setNewData(examQuestion.getResourceList());
        setNavigationShowImage(subjectImgAdapter);

        if (isExamResult()) {
            rbAnswerError.setEnabled(false);
            rbAnswerSuccess.setEnabled(false);

            if (examQuestion.getGetScore() > 0){
                ivSubjectState.setImageResource(R.mipmap.ic_subject_success);
            }else {
                ivSubjectState.setImageResource(R.mipmap.ic_subject_error);
            }
        }

        if (examQuestion.getSubjectAnswer() != null) {
            examQuestion.setStudentAnswer(examQuestion.getSubjectAnswer().getAnswer());
        }

        if (isShowResult()) {
            ivSubjectState.setVisibility(View.GONE);
            answer = examQuestion.getCorrectAnswer();
            rbAnswerSuccess.setButtonDrawable(R.drawable.result_subject_rb_state);
            rbAnswerError.setButtonDrawable(R.drawable.result_subject_rb_state);

        }else{
            if (isExamResult()) {
                ivSubjectState.setVisibility(View.VISIBLE);
            }
            answer = examQuestion.getStudentAnswer();
        }
        if (ObjectUtils.equals(q1, examQuestion.getStudentAnswer())) {
            rbAnswerSuccess.setChecked(true);
        }else if (!TextUtils.isEmpty(examQuestion.getStudentAnswer())){
            rbAnswerError.setChecked(true);
        }
        if (!TextUtils.isEmpty(answer)) {
            subjectAnswer.setAnswer(examQuestion.getStudentAnswer());
        }
        /*if (getUserSubjectAnswer() != null) {
            subjectAnswer = getUserSubjectAnswer();
        }*/
        callbackResult(subjectAnswer);
    }

    private void setOnListnener() {
        rgAnswerGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_answer_success:
                        subjectAnswer.setAnswer(q1);
                        break;
                    case R.id.rb_answer_error:
                        subjectAnswer.setAnswer(q2);
                        break;
                }
                if (getActivity() != null) {
                    if (getActivity() instanceof AnswerCallback) {
                        ((AnswerCallback) getActivity()).callback(subjectAnswer);
                    }
                }
            }
        });
    }
}
